﻿layui.define(['jquery', 'layer'], function (exports) {
    var $ = layui.jquery;
    var layer = layui.layer
    var obj = {
        open: function (options) {
            var D_obj = {
                text: '未传入提示内容',
                time: 5,
                suc: false,
                tab_refresh: '',    //刷新指定标签页
                ref: [],    //刷新指定 iframe
                table_ref: '',   //刷新指定标签页内 table
                close: true,    //关闭本页
                reload: false,  //刷新本页
                tabi: null, //需要被关闭的tab index
                fun: null, //成功事件
                erfun: null, //失败事件
                done: null //倒数结束执行事件（无关suc）
            }
            if (typeof options == 'string') {
                if (options.length > 200) { options = options.slice(0, 200) }
                D_obj.text = options
            } else {
                $.extend(D_obj, options)
            }
            var icons = "data:image/svg+xml,%3Csvg class='icon' viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg' width='32' height='32'%3E%3Cpath d='M43.072 974.72l380.864-301.952 151.936 161.6s63.424 17.28 67.328-30.72l-3.904-163.584 225.088-259.648 98.048-5.696s76.928-15.488 21.184-82.752L708.544 15.04s-74.944-9.6-69.248 59.584v75.008L383.552 367.104l-157.696 9.536s-57.728 19.2-36.608 69.248l148.16 146.176L43.072 974.72z' fill='%23fff'/%3E%3C/svg%3E",
                iconh = "data:image/svg+xml,%3Csvg class='icon' viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg' width='32' height='32'%3E%3Cpath d='M43.072 974.72l380.864-301.952 151.936 161.6s63.424 17.28 67.328-30.72l-3.904-163.584 225.088-259.648 98.048-5.696s76.928-15.488 21.184-82.752L708.544 15.04s-74.944-9.6-69.248 59.584v75.008L383.552 367.104l-157.696 9.536s-57.728 19.2-36.608 69.248l148.16 146.176L43.072 974.72z' fill='%23009688'/%3E%3C/svg%3E"
            var content = '<div id="mymsg" class="layui-layer-content"><div class="msg_header"><div class="msg_time" style="position:absolute;left:35px;top:8px;cursor:default;">' + D_obj.time + '</div><span class="msg_icon_parent" style="position:absolute;cursor:pointer;right:auto;left:0px;top:0px;"><img class="msg_icon" style="width:16px;margin:8px;" src="' + icons + '"></span></div><div class="msg_content" style="padding:20px 30px;overflow-x: hidden;text-overflow: ellipsis;">' + D_obj.text + '</div></div>'
            var timeInterval = false
            //提前修改样式，以保证 layer.open 样式计算正确
            //var msgstyle = '<style>div.mymsg > div.layui-layer-content{padding:0;overflow:hidden;}div.mymsg{max-width:80vw;}</style>';
            //$('head').append(msgstyle)
            layer.open({
                type: 1,
                title: false,
                content: content,
                time: 0,
                closeBtn: 1,
                shadeClose: true,
                shade: 0.01,
                skin: 'mymsg layui-layer-msg layui-layer-hui',
                success: function (layero, index) {
                    var msg_time = D_obj.time
                    var $msg_time = $('#mymsg .msg_time')
                    var $icon_parent = $('#mymsg .msg_icon_parent')

                    !timeInterval || clearInterval(timeInterval)
                    timeInterval = setInterval(function () {
                        if (msg_time == 1) {
                            clearInterval(timeInterval)
                            layer.close(index)
                            return
                        }
                        msg_time = msg_time - 1
                        $msg_time.text(msg_time)
                    }, 1000)

                    $icon_parent.hover(function () {
                        if ($msg_time.html() == '&nbsp;') { return }
                        $(this).find('img').attr('src', iconh)
                    }, function () {
                        if ($msg_time.html() == '&nbsp;') { return } else {
                            $(this).find('img').attr('src', icons)
                        }
                    })

                    $icon_parent.on('click', function () {
                        $(this).find('img').attr('src', iconh)
                        if (!timeInterval) { return }
                        clearInterval(timeInterval)
                        timeInterval = false
                        $msg_time.html('&nbsp;')
                    })

                },
                end: function () {
                    !timeInterval || clearInterval(timeInterval)
                    if (!D_obj.suc) { !D_obj.erfun || D_obj.erfun(); !D_obj.done || D_obj.done();return }   //执行失败事件
                    !D_obj.fun || D_obj.fun();  //执行成功事件
                    !D_obj.tab_refresh || tab_refresh(D_obj.tab_refresh);   //刷新指定标签页
                    (D_obj.ref.length == 0) || iframe_refresh(D_obj.ref[0], D_obj.ref[1]);  //刷新指定iframe
                    !D_obj.table_ref || reload_table(D_obj.table_ref);   //刷新指定标签页内 table 
                    if (!!D_obj.tabi) {     //关闭指定标签页（index）
                        setTimeout(function () {
                            top.layui.$('#LAY_app_tabsheader>li').eq(D_obj.tabi).find(".layui-tab-close").trigger("click")
                        }, 1000)
                        return
                    }
                    if (!!D_obj.reload) {   //刷新本页
                        window.location.reload()
                        return;
                    }
                    !D_obj.close || top.layui.admin.events.closeThisTabs(); //关闭本页
                }
            })
        },
        confirm: function (options) {
            var D_obj = {
                text: '确定执行操作吗？',
                options: {
                    title: ' ',
                    //shade: [0.3, '#7b7a7a1c'],
                    shade: 0,
                    skin: 'timeConfirm',
                    end: function () {
                        !timeInterval || clearInterval(timeInterval);
                    }
                },
                yes: null,
                cancel: null,
                time: 9,
                countdownApp: 'cancel'  //在按钮倒计时自动点击, 默认为 取消按钮
            },
                timeInterval = false
            $.extend(true, D_obj, options)

            var index = layer.confirm(D_obj.text, D_obj.options, D_obj.yes, D_obj.cancel)

            var $layer = $('#layui-layer' + index), $msg_icon_parent = $layer.find('.layui-layer-setwin').addClass('msg_icon_parent').html('<svg class="icon" viewBox="0 0 1106 1024" xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path d="M76.357 1024l387.32-307.09 154.56 164.335s64.553 17.522 68.518-31.355l-3.965-166.363 228.98-263.93 99.688-5.81s78.202-15.677 21.58-84.196L753.244 47.954s-76.265-9.775-70.455 60.588v76.265L422.732 406.04l-160.461 9.683s-58.744 19.55-37.257 70.456L375.7 634.836 76.357 1024z"/></svg>')
            var $btns = $layer.find('.layui-layer-btn > a'),
                $msg_timeBtn = D_obj.countdownApp == 'yes' ? $btns.first() : $btns.last(),
                btnText = $msg_timeBtn.text()
            $msg_timeBtn.html(btnText + '（<span class="msg_time">' + D_obj.time + '</span>）')
            $msg_timeBtn.addClass('hasMsg_time')
            var $msg_time = $msg_timeBtn.find('.msg_time')
            //倒计时
            !timeInterval || clearInterval(timeInterval)
            timeInterval = setInterval(function () {
                if (D_obj.time === 1) {
                    $msg_timeBtn.click()
                    clearInterval(timeInterval)
                    timeInterval = false
                    return
                }
                D_obj.time--
                $msg_time.text(D_obj.time)
            }, 1000)

            //图钉图标点击取消计时
            $msg_icon_parent.on('click', function () {
                if (!timeInterval) { return }
                $(this).addClass('active')
                clearInterval(timeInterval)
                timeInterval = false
                $msg_timeBtn.text(btnText)
                $msg_timeBtn.removeClass('hasMsg_time')
            })
        }
    };
    //输出接口
    exports('timemsg', obj);
});